// 配置路由规则
import Vue from 'vue'
import Home from '@/components/home/Home.vue'
import Login from '@/components/login/Login.vue'
import Users from '@/components/users/Users.vue'
import Roles from '@/components/roles/Roles.vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
Vue.config.productionTip = false
const router = new VueRouter({
  // 配置路由规则
  routes: [
    {path: '/login', component: Login},
    {
      path: '/home',
      component: Home,
      children:[
        {path: '/users', component: Users},
        {path: '/roles', component: Roles}
      ]
    },
    

  ]
})
// vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航
// 判断是否是登录的，登录的放行，未登录的去登录，判断是否登陆过，登录过的放行，未登录过的去登录
router.beforeEach((to, from, next) => {
  // console.log(to.path)
  if (to.path === '/login') {
    return next()
  }
  const token = localStorage.getItem('token')
  // console.log(token)
  if (token) {
    next()
  } else {
    next('/login')
  }
})
export default router
